//
//  HomeCell.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/6.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit
import SnapKit

class HomeCell: UITableViewCell {
    
    var footerViewConstrant: Constraint?
    
    var personalBlogViewModel: PersonalBlogViewModel? {
        
        didSet {
            
            originalBlogView.originalBolgModel = personalBlogViewModel
            footerView.footerViewModel = personalBlogViewModel
            
            footerViewConstrant?.uninstall()
            
            if personalBlogViewModel?.statuse?.retweeted_status == nil {
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    footerViewConstrant = make.top.equalTo(originalBlogView.snp_bottom).constraint
                })
                retweetBlogView.hidden = true
                
            } else {
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    footerViewConstrant = make.top.equalTo(retweetBlogView.snp_bottom).constraint
                })
                retweetBlogView.retweetBlogViewModel = personalBlogViewModel
                retweetBlogView.hidden = false
            }
        }
    }
    
   private lazy var originalBlogView: OriginalBolgView = OriginalBolgView()
    
   private lazy var footerView: FooterView = FooterView()
    
   private lazy var retweetBlogView: RetweetBlogView = RetweetBlogView()
    
   private override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    contentView.backgroundColor = UIColor(white: 240 / 255, alpha: 1.0)
    
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        
        contentView.addSubview(originalBlogView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetBlogView)
        
        originalBlogView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView.snp_top).offset(OriginalBlogMargin)
            make.left.equalTo(contentView.snp_left)
            make.width.equalTo(contentView.snp_width)
            
        }
        
        retweetBlogView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalBlogView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.width.equalTo(contentView.snp_width)
        }
        
        footerView.snp_makeConstraints { (make) -> Void in
            
            footerViewConstrant = make.top.equalTo(retweetBlogView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.width.equalTo(contentView.snp_width)
            make.height.equalTo(44)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(footerView.snp_bottom)
            
            make.width.equalTo(self.snp_width)
            
            make.top.equalTo(self.snp_top)
            
            make.left.equalTo(self.snp_left)
        }
        
    }
    
    
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
